import axios from "@/utils/http"
import { ThematicStore } from "@/stores/BackData/Thematic/Thematic";
import {getHMS} from "@/utils/time"

const store= ThematicStore();

// 获取全部
export function getAllThematic(thematicName?:any,isShelves?:any){
    // console.log(token);
    
    return axios({
        method:"post",
        url:"/back/thematic/getThematicList",
        data:JSON.stringify({
            "isShelves": isShelves,
            "limit": 20,
            "page": 1,
            "thematicName": thematicName
        }),
         headers: {
            'Content-Type': 'application/json;charset=UTF-8'
         }
    }).then((res) => {

        if (res.data.msg) {
          store.pushAllDate(res.data.data);
          
        }
      });
}
//清空全部
export function delAllThematic(){
    // console.log(token);
    
    store.thematicInfo.splice(0,store.thematicInfo.length);
    // return(1111)
}

// 获取部分
export function getPartThematic(page?:any,thematicName?:any,isShelves?:any){
    // console.log(token);
    
    return axios({
        method:"post",
        url:"/back/thematic/getThematicList",
        data:JSON.stringify({
            "isShelves": isShelves,
            "limit": 4,
            "page": page,
            "thematicName": thematicName
        }),
         headers: {
            'Content-Type': 'application/json;charset=UTF-8'
         }
    }).then((res) => {

        if (res.data.msg) {
          store.pushPartDate(res.data.data);
          
        }
      });
}
//清空部分
export function delPartThematic(){
    // console.log(token);
    
    store.partthematicInfo.splice(0,store.thematicInfo.length);
    // return(1111)
}


// 创建专题
export function createPartThematic(createthematicName?:any,createthematicDescription?:any,createcategoryId?:any){
    // console.log(token);
    
    return axios({
        method:"post",
        url:"/back/thematic/addThematic",
        data:{
            "categoryId": createcategoryId,
            "id": null,
            "thematicDescription": createthematicDescription,
            "thematicImg": "",
            "thematicName": createthematicName
        },
         headers: {
            'Content-Type': 'application/json;charset=UTF-8'
         }
    }).then((res) => {
        console.log(res.data.msg);
        getPartThematic();
      });
}


//上架专题
export function addisShelvesThematic(id?:any,isShelves?:any){
    // console.log(token);
    
    return axios({
        method:"post",
        url:"/back/thematic/updateThematic",
        params:{
            "id": id,
            "isShelves": isShelves,
        },
        data:JSON.stringify({
            "id":id,
            "isShelves": isShelves,
        }),
         headers: {
            'Content-Type': 'application/json;charset=UTF-8'
         }
    }).then((res) => {
        console.log(res.data.msg);
        
        if (res.data.code==0) {
          store.nowthematicInfo[0].isShelves = isShelves;
          idgetPartThematic(id)
          
        }
      });
}


//删除专题
export function deltheThematic(id?:any){
    // console.log(token);
    
    return axios({
        method:"DELETE",
        url:"/back/thematic/deleteThematic",
        params:{
            "id": id,

        },
        data:JSON.stringify({
            "id":id,

        }),
         headers: {
            'Content-Type': 'application/json;charset=UTF-8'
         }
    }).then((res) => {
        console.log(res.data.msg);
        getPartThematic();
      });
}

//修改专题
export function updatetheThematic(id?:any,thematicName?:any,thematicDescription?:any,categoryId?:any,thematicImg?:any){
    // console.log(token);
    
    return axios({
        method:"PUT",
        url:"/back/thematic/updateThematic",
        params:{
            "categoryId": categoryId,
            "id": id,
            "thematicDescription": thematicDescription,
            "thematicImg": thematicImg,
            "thematicName": thematicName
        },
        data:JSON.stringify({
            "categoryId": categoryId,
            "id": id,
            "thematicDescription": thematicDescription,
            "thematicImg": thematicImg,
            "thematicName": thematicName

        }),
         headers: {
            'Content-Type': 'application/json;charset=UTF-8'
         }
    }).then((res) => {
        console.log(res.data.msg);
        getPartThematic();
        idgetPartThematic(id)
      });
}

///根据id查询专题
export function idgetPartThematic(id:number){
    //console.log(token);
    console.log(id);
    
    return axios({
        method:"post",
        url:"/back/thematic/getThematicById",
        params:{
            "id": id,
        },
         headers: {
            'Content-Type': 'application/json;charset=UTF-8'
         }
    }).then((res) => {
        // console.log(res);
        
        if (res.data.msg=="请求成功") {
          store.pushIdDate(res.data.data);
          
        }
      });
}